package com.hongkuncheng.cms.controller.index;

import com.alibaba.fastjson.JSONObject;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.ArticleHelper;
import com.hongkuncheng.cms.helper.CookieHelper;
import com.hongkuncheng.cms.model.Article;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Transactional
@RequestMapping("article")
@ResponseBody
public class IarticleController extends BaseController {

    @RequestMapping("getclicknumber")
    public void getclicknumber() {
        int id = Integer.parseInt(request.getParameter("id"));
        if (CookieHelper.GetCookie(request, "articleclicknum_" + id, false) == null) {
            Article article = factorySession.get(Article.class, id);
            article.setClicknum(article.getClicknum() + 1);
            CookieHelper.SetCookie(response, "articleclicknum_" + id, String.valueOf(article.getClicknum()),
                    1200, false);
            print(article.getClicknum());
        }
    }

    @RequestMapping("getcontents")
    public void getcontents() {
        JSONObject object = new JSONObject();
        String checkcode = CookieHelper.GetCookie(request, "checkcode", true);
        if (checkcode == null) {
            object.put("state", -1);
        } else {
            String vcode = request.getParameter("vcode");
            if (!vcode.equalsIgnoreCase(checkcode)) {
                object.put("state", -2);
            } else {
                int id = Integer.parseInt(request.getParameter("id"));
                Article article = factorySession.get(Article.class, id);
                if (article.isProtection()) {
                    if (article.getProtectionpassword().equals(request.getParameter("password"))) {
                        object.put("state", 1);
                        object.put("digest", article.getDigest());
                        object.put("contents", ArticleHelper.GetPageContents(article.getContents()));
                    } else {
                        object.put("state", 0);
                    }
                }
            }
            CookieHelper.RemoveCookie(response, "checkcode");
        }
        print(object.toString());
    }

}
